﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace COFE.Core.Utils
{
    public class DelegateFactory<T> : IFactory<T>
    {
        #region Constructor

        public DelegateFactory(string factoryKey, Func<string, string, T> constructMethod, Func<T, string> recycleMethod = null)
        {
            Assert.IsNotNull(constructMethod);
            Assert.IsNotNullOrEmpty(factoryKey);
            FactoryKey = factoryKey;
            _constructMethod = constructMethod;
            _recycleMethod = recycleMethod;
        }

        #endregion

        #region Methods

        public T Construct(string id, string paramString)
        {
            return _constructMethod(id, paramString);
        }

        public string Recycle(T product)
        {
            if (_recycleMethod != null)
                return _recycleMethod(product);
            return "";
        }
        #endregion

        #region Data
        Func<string, string, T> _constructMethod = null;
        Func<T, string> _recycleMethod = null;        
        #endregion

        #region Public Properties  
      
        public string FactoryKey { get; private set; }

        #endregion
    }

}
